package com.slkj.shilixiaoyuanapp.ui.parent.tool.exercise;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.google.gson.Gson;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.UserEntity;
import com.slkj.shilixiaoyuanapp.entity.parent.AddExerciseEntity;
import com.slkj.shilixiaoyuanapp.entity.parent.ExerciseTypeEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.parent.adapter.ExerciseDetailAdapter;
import com.slkj.shilixiaoyuanapp.view.PickerViewProvider;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.NO_CHOSE;

@ActivityInfo(layout = R.layout.activity_exer,title = "平时锻炼",rightHint = "历史记录")
public class ExerciseActivity extends BaseActivity {
    @BindView(R.id.rl_student_name)
    RelativeLayout rl_student_name;
//    @BindView(R.id.rv_exercise_detail)
//    RecyclerView rv_exercise_detail;
//    @BindView(R.id.ll_add_exercise)
//    LinearLayout ll_add_exercise;
    @BindView(R.id.tv_student_name)
    TextView tv_student_name;
    @BindView(R.id.tv_exercise_pro)
    TextView tv_exercise_pro;
    @BindView(R.id.et_exercise_long)
    EditText et_exercise_long;
    //ExerciseDetailAdapter exerciseDetailAdapter;
    private List<AddExerciseEntity> list=new ArrayList<>();
    private List<String> pro=new ArrayList<>();
    private List<String> time=new ArrayList<>();
    //班级id
    private int classId = NO_CHOSE;
    //学生id
    private int studentId = NO_CHOSE;
    List<ExerciseTypeEntity> cacheData;
    AddExerciseEntity addExerciseData;
    int id;
    String project;

    @Override
    protected void init() {
        //getData();
//        if (UserRequest.getInstance().getUser().getStudents().size()!=0) {
//            tv_student_name.setText(UserRequest.getInstance().getUser().getStudents().get(0).getName());
//        }
        //list.add("a");
        //exerciseDetailAdapter=new ExerciseDetailAdapter(ExerciseActivity.this,list);
        //rv_exercise_detail.setLayoutManager(new LinearLayoutManager(this));
        //rv_exercise_detail.setAdapter(exerciseDetailAdapter);
//        exerciseDetailAdapter.setOnTextChangeListener(new ExerciseDetailAdapter.OnTextChangeListener() {
//            @Override
//            public void onTextChange(List<String> proList, List<String> longList) {
//                Log.e("===========",proList.toString()+longList.toString());
//                pro=proList;
//                time=longList;
//            }
//        });
        for (int i=0;i<UserRequest.getInstance().getUser().getStudents().size();i++){
            if (UserRequest.getInstance().getUser().getStudents().get(i).getIsSelect()==1){
                studentId=UserRequest.getInstance().getUser().getStudents().get(i).getId();
                classId=UserRequest.getInstance().getUser().getStudents().get(i).getClassId();
                tv_student_name.setText(UserRequest.getInstance().getUser().getStudents().get(i).getName());
            }
        }
    }

    private void getData() {
        HttpHeper.get().parentService().getExerciseType().compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<List<ExerciseTypeEntity>>(true, this) {
                    @Override
                    public void onCallBackSuccess(List<ExerciseTypeEntity> data) {
                        cacheData = data;
                    }
                });
    }

    @Override
    protected void onRightTitleClick() {
        startActivity(ExerciseHistoryActivity.class);
    }

//    @OnClick(R.id.rl_student_name)
//    public void onLayoutSelectStudent(){
//        List<UserEntity.StudentEntity> students = UserRequest.getInstance().getUser().getStudents();
//        PickerViewProvider.getCommonPicker(this, students, new OnOptionsSelectListener() {
//            @Override
//            public void onOptionsSelect(int options1, int options2, int options3, View v) {
//                tv_student_name.setText(students.get(options1).getName());
//                studentId = students.get(options1).getId();
//                classId  = students.get(options1).getClassId();
//            }
//        }).show();
//    }

    @OnClick(R.id.layout_exercise_pro)
    public void onLayoutSelectExerciseType() {
        HttpHeper.get().parentService().getExerciseType().compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<List<ExerciseTypeEntity>>(true, this) {
                    @Override
                    public void onCallBackSuccess(List<ExerciseTypeEntity> data) {
                        cacheData = data;
                        List<ExerciseTypeEntity> list = new ArrayList<>();
                        list.addAll(cacheData);
                        PickerViewProvider.getCommonPicker(ExerciseActivity.this, list, new OnOptionsSelectListener() {
                            @Override
                            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                                tv_exercise_pro.setText(list.get(options1).getProject());
                                project = list.get(options1).getProject();
                                id = list.get(options1).getId();
                            }
                        }).show();
                    }
                });
    }

//    @OnClick(R.id.ll_add_exercise)
//    public void addExerciseDetail(){
//        list.add("a");
//        exerciseDetailAdapter.notifyDataSetChanged();
//    }
    @OnClick(R.id.tv_up)
    public void up(){
        if (studentId == NO_CHOSE) {
            showToast("请选择学生");
            return;
        }
        String exercisePro=tv_exercise_pro.getText().toString();
        if (TextUtils.isEmpty(exercisePro)){
            showToast("请选择锻炼项目");
            return;
        }
        String exerciseLong=et_exercise_long.getText().toString();
        if (TextUtils.isEmpty(exerciseLong)){
            showToast("请填写锻炼时长");
            return;
        }
        addExerciseData=new AddExerciseEntity();
        addExerciseData.setId(studentId);
        addExerciseData.setProject(project);
        addExerciseData.setHours(Double.parseDouble(exerciseLong));
        list.add(addExerciseData);
        Log.e("==============",new Gson().toJson(new Gson().toJson(list)));
        HttpHeper.get().parentService().addExercise(studentId, classId, new Gson().toJson(list))
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true, this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(ExerciseActivity.this, data);
                        clearContent();
                    }
                });
    }
}
